package com.zoweunion.mechanic.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/*
 * auth: 王春伟
 */
@Configuration 
@ConfigurationProperties(prefix = "context")
@PropertySource(value = {"classpath:myConfig.properties"})
public class MyWebAppConfigurer implements WebMvcConfigurer {  
	
	private String image_upload_path;

    public String getImage_upload_path() {
		return image_upload_path;
	}

	public void setImage_upload_path(String image_upload_path) {
		this.image_upload_path = image_upload_path;
	}

	@Override  
    public void addCorsMappings(CorsRegistry registry) {  
        registry.addMapping("/**")  
                .allowedOrigins("*")  
                .allowCredentials(true)  
                .allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")  
                .maxAge(3600);  
    }  
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    	
    	String os = System.getProperty("os.name");

    	   if (os.toLowerCase().startsWith("win")) {  //如果是Windows系统
    	      registry.addResourceHandler("/image/**")
    	            .addResourceLocations(image_upload_path) //媒体资源
    	            .addResourceLocations("classpath:/META-INF/resources/");  //swagger2页面
    	   } else {  //linux 和mac
    	      registry.addResourceHandler("/image/**")
    	            .addResourceLocations(image_upload_path)   //媒体资源
    	            .addResourceLocations("classpath:/META-INF/resources/");  //swagger2页面;
    	   }
    }
    
}  
